define(['underscore', 'jquery', 'Loader'], function (_, $, Loader) {
	var yfUtil = {
		yfUtil_init: function () {
			var reWriteConsole = false; //例如生产环境要禁止console
			this.reWriteConsoleFn(reWriteConsole);
			this.reWriteOverall();
			this.linkCssFn();
			this.verification();
			this.lazyLoad();
			this.setShowToast();
		},
		//重写一些window方法 test
		reWriteOverall: function () {
			//缓存
			window.setLocalStrogeItem = function (key, value) {
				typeof value != "string" ? localStorage.setItem(key, JSON.parse(value)) : localStorage.setItem(key, value);
			}
			window.getLocalStrogeItem = function (key) {
				var data = JSON.parse(localStorage.getItem(key));
			}
			//计算精度问题
			//trim()
			String.prototype.trim = function () {
				return this.replace(/(^\s*)|(\s*$)/g, "");
			}
			//日期格式化
			//表单验证
		},
		linkCssFn: function () {
			function addCss(url) {
				//判断是否引入公共css，没有的话就动态引入
				var a = false,
					link, style;
				var sheets = document.styleSheets;
				for (var i = 0, l = sheets.length; i < l; i++) {
					if (sheets[i].href && sheets[i].href.indexOf(url) !== -1) {
						a = true;
						break;
					}
				}
				if (!a) {
					link = document.createElement("link");
					link.rel = "stylesheet";
					link.href = url;
					document.head.appendChild(link);
				}
			}
			addCss("../../common/css/common.css");
			addCss("../../common/css/yfUI.css");
			addCss("http://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css");
		},
		reWriteConsoleFn: function (reWriteConsoleArg) {
			window._console = window.console;
			if (reWriteConsoleArg) { //如果是生产环境
				var n = function () {};
				window.console = {
					assert: n,
					clear: n,
					count: n,
					debug: n,
					dir: n,
					dirxml: n,
					error: n,
					group: n,
					groupCollapsed: n,
					groupEnd: n,
					info: n,
					log: n,
					markTimeline: n,
					profile: n,
					profileEnd: n,
					table: n,
					time: n,
					timeEnd: n,
					timeStamp: n,
					timeline: n,
					timelineEnd: n,
					trace: n,
					warn: n
				}
			}
		},
		//用于tap切换,不成熟，废弃
		transTap: function (e, container, createFn) {
			//页面传一个函数过来，这个函数是带有ajax的，
			//yfUtil传一个函数回去，带有处理ajax数据的，
			//总作用就是yfUtil判断要不要页面去ajax，如果要，还要拿到页面的ajax数据来处理
			//当插件不清楚要不要页面发送ajax，而且这个ajax是由页面来调，插件还要用到返回的数据的时候可以这样写
			if ($(e.target).attr('data-transTap')) {
				var transTap = $(e.target).attr('data-transTap');
				container.find('.transTap').hide();
				container.find("#" + transTap).show();
			} else {
				container.find('.transTap').hide();
				createFn(function (e, container, tpl, data) {
					var transTap = $('<div></div>');
					var timestamp = new Date().getTime();
					$(e.target).attr('data-transTap', timestamp);
					transTap.attr('id', timestamp);
					transTap.addClass('transTap');
					transTap.css('width', '100%');

					yfUtil.createTemplate(tpl, data, transTap);
					container.append(transTap);
				});
			}
		},
		ajax: function (obj) {
			jQuery.support.cors = true; //支持cors跨域
			var paramsObj = obj;
			var myLoader;
			//最好先判断网络连接情况
			$.ajax({
				timeout: paramsObj.timeout,
				url: paramsObj.url,
				type: paramsObj.type,
				data: paramsObj.data,
				dataType: paramsObj.dataType,
				async: true,
				beforeSend: function (xhr, settings) {
					console.log(xhr);
					console.log(settings);
					//可以在这里加载loader
					//myLoader = new Loader();
				},
				success: function (data, textStatus, jqXHR) {
					if (typeof data == "string") {
						data = JSON.parse(data);
					}
					paramsObj.success(data, textStatus, jqXHR);
				},
				dataException: function () {
				},
				error: function (e, xhr, type) {
					if (paramsObj.error && paramsObj.error instanceof Function) {
						paramsObj.error(e, xhr, type);
					}
				},
				complete: function (xhr, status) {
					//做一些成功之后的判断,例如销毁loader
					//myLoader.remove();
					if (paramsObj.complete && paramsObj.complete instanceof Function) {
						paramsObj.complete(xhr, status);
					}
				}
			});
		},
		verification: function () {
			//   RegExp 对象有 3 个方法：
			//  		test()：返回true或者false
			//   		exec()：返回检索到的值或者null(数组)
			//   		compile():改变RegExp的值
			//   String 对象的方法： 
			//   		indexOf():定位字符串中某一个指定的字符首次出现的位置
			//   		match():查找字符串中特定的字符，并且如果找到的话，则返回这个字符
			//   		replace():在字符串中用某些字符替换另一些字符(不用循环)
			//  需求：   1，我要验证邮箱和电话号码是否正确，返回true或者false 
			//				isXXX,用到的方法：pattern.test(str);
			//  		2，我要限制输入的内容，筛选出我想要的东西，比如只能输入中文，英文 
			//				getXXX,用到的方法，str.match(reg),返回的是数组，必须join
			// 		    3，我要去掉某些内容，比如把一些敏感字过滤掉	(remove)
			//				removeXXX,用到的方法str.replace(reg,'');
			var overAllReg = {
				isMail: "判断邮箱",
				isPhone: "判断手机",
				removeSpecial: "过滤敏感字符(可传自定义敏感字符)",
				getNeed: "只能输入中文英文数字"
			}
			window.overAllReg = overAllReg;
			//判断是否为邮箱
			String.prototype.isMail = function () {
				var pattern = /mail/;
				var _isMail = pattern.test(this);
				return _isMail;
			}
			//判断是否为手机
			String.prototype.isPhone = function () {
				var pattern = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;
				var _isPhone = pattern.test(this);
				return _isPhone;
			}
			//过滤敏感字符
			String.prototype.removeSpecial = function () {
				var arrMg = ['fuck', 'tmd',' ',' ','¥','€','£','•','&'];
				var str = this;
				for (var i = 0; i < arrMg.length; i++) {
					var r = new RegExp(arrMg[i], "ig");
					str = str.replace(r, '');
				}
				return str;
			}
			//过滤emoji表情
			String.prototype.removeEmoji = function () {
				var r = /\ud83c[\udf00-\udfff]|\ud83d[\udc00-\ude4f]|\ud83d[\ude80-\udeff]/g;
				var str = this.replace(r, '');
				return str;
			}
			//比如用户名不能输入除了中文英文数字之外
			String.prototype.getNeed = function () {
				var reg = /[\u2E80-\u9FFF]|\w|[,，!！\.。]|[\uac00-\ud7ff]/g;
				var str = "";
				var arr = this.match(reg);
				str = arr.join("");
				return str;
			}
			//中文：[\u4e00-\u9fa5]，英文数字：[0-9a-zA_Z]
			String.prototype.getNeedCYD = function () {
				var reg = /[\u4e00-\u9fa5]|[0-9a-zA-Z]|[\,\，\!\！\.\。\~\^\_\-]/g;
				var str = "";
				var arr = this.match(reg);
				str = arr.join("");
				return str;
			}
			//如果不符合中文英文标点就为false
			String.prototype.isNeed = function () {
				var reg = /^[0-9a-zA-Z | \u4e00-\u9fa5 | \,\，\!\！\.\。\~\^\_\-]+$/;
				var boo = reg.test(this);
				return boo;
			}
			//替换非法字符
			String.prototype.getGb2312 = function(){
			    var reg = /[^\u4e00-\u9fa5\w]/g;
			    var str = this.replace(reg, "");
			    return str;
			}
		},
		lazyLoad: function () {
			/*
			var imgs = $('img');
			var myLazyLoad = new LazyLoad(imgs);
			myLazyLoad.init();
			*/
			var LazyLoad = function (container,imgs) {
				this.imgs = imgs;
				this.container = $(container);
				//注意container要有relative或者absolute的定位
				this.create();
			}
			LazyLoad.prototype = {
				constructor: LazyLoad,
				create: function () {
					var that = this;
					this.container.on('scroll',function(){
						var imgs = that.imgs;
						for (var i = 0; i < imgs.length; i++) {
							//divH：图片距离文档顶部距离
							var divH = that.getDivHeight(imgs[i]);
							//screenH：屏幕高度+文档卷起高度
							var screenH = that.getScreenHeight();
							if (divH<screenH) {
								var src = $(imgs[i]).attr('data-src')
								imgs[i].src = src;
							}
						}
					});
				},
				init:function(){
					$(this.container).scroll();
				},
				//获得对象距离页面顶端的距离 
				getDivHeight:function(ele){
					var h = 0;
					while(ele){
						h+=ele.offsetTop;
						//获取距离容器顶部距离
						if(ele==this.container[0]){
							break;
						}
						ele = ele.offsetParent;
					}
					return h;
				},
				//screenH：屏幕高度+文档卷起高度
				getScreenHeight:function(){
					var h = 0;
					//h = document.documentElement.clientHeight + (document.documentElement.scrollTop||document.body.scrollTop);
					h = document.documentElement.clientHeight + this.container[0].scrollTop;
					return h;
				}
			}
			window.LazyLoad = LazyLoad;
		},
		//提示框
		setShowToast:function(){
			var ShowToast = function(txt,time){
				this.txt = txt?txt:"";
				this.time = time?time:1200;
				this.init();
			}
			ShowToast.prototype = {
				constructor:ShowToast,
				init:function(){
					if($('.js_showToast').length!=0){
						return;
					}
					var showToast = $('<div class="js_showToast"></div>');
					showToast.css({
						position:"absolute",
						top:"30%",
						left:"50%",
						width:"200px",
						background:"black",
						opacity:"0.7",
						transform:"translate(-50%,-50%)",
						color:"white",
						textAlign:"center",
						padding:"10px 10px",
						boxSizing:"border-box",
						zIndex:"9999"
					});
					showToast.html(this.txt);
					$('body').append(showToast);
					setTimeout(function(){
						showToast.fadeOut("normal",function(){
							showToast.remove();
						});
					},this.time);
				}
			}
			window.ShowToast = ShowToast;
		}
	}
	yfUtil.yfUtil_init();
	return yfUtil;
})